import dayjs from "dayjs";
import {ref, computed, onUnmounted} from 'vue'

export const useCountDown = () => {
    let timer = null
    const time = ref(0)
    // 使用计算属性获取格式化后的倒计时
    const formatTime = computed(() => {
        return dayjs.unix(time.value).format('mm分ss秒')
    })


    const start = (seconds) => {
        time.value = seconds
        timer = setInterval(() => {
            time.value--
        }, 1000)
    }
    onUnmounted(() => {
        timer && clearInterval(timer)
    })
    return {
        formatTime,
        start
    }
}